package com.example.bingobox.view;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.LinearGradient;
import android.graphics.Paint;
import android.graphics.Rect;
import android.graphics.Shader;
import android.util.AttributeSet;
import android.util.TypedValue;
import android.view.MotionEvent;
import android.view.View;

public class MoodSelector extends View {
    private Paint gradientPaint;
    private Paint indicatorPaint;
    private Paint textPaint;
    private float indicatorX = -1;  // -1 表示没有选择
    private int viewWidth;
    private int viewHeight;
    private static final int INDICATOR_WIDTH = 4;  // 指示线宽度
    private static final String[] MOODS = {"愤怒", "兴奋", "平定", "倦怠", "忧郁", "迷茫"};
    private OnMoodSelectedListener listener;
    private String currentMood = "";
    private final Rect textBounds = new Rect();

    public MoodSelector(Context context) {
        super(context);
        init();
    }

    public MoodSelector(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    private void init() {
        gradientPaint = new Paint();
        gradientPaint.setAntiAlias(true);

        indicatorPaint = new Paint();
        indicatorPaint.setColor(Color.WHITE);
        indicatorPaint.setStrokeWidth(INDICATOR_WIDTH);
        indicatorPaint.setAntiAlias(true);

        // 初始化文字画笔
        textPaint = new Paint();
        textPaint.setColor(Color.WHITE);
        textPaint.setAntiAlias(true);
        textPaint.setTextAlign(Paint.Align.CENTER);
        // 将文字大小设置为12sp
        float textSizeInSp = 12;
        float textSizeInPx = TypedValue.applyDimension(
            TypedValue.COMPLEX_UNIT_SP,
            textSizeInSp,
            getResources().getDisplayMetrics()
        );
        textPaint.setTextSize(textSizeInPx);
    }

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);
        viewWidth = w;
        viewHeight = h;

        // 创建渐变色
        int[] colors = new int[]{
            Color.rgb(200, 0, 0),    // 深红色
            Color.rgb(255, 165, 0),  // 橙黄色
            Color.rgb(144, 238, 144),// 浅绿色
            Color.rgb(0, 206, 209),  // 蓝绿色
            Color.rgb(25, 25, 112),  // 深蓝色
            Color.rgb(128, 128, 128) // 灰色
        };

        LinearGradient gradient = new LinearGradient(
            0, 0, viewWidth, 0,
            colors, null,
            Shader.TileMode.CLAMP
        );
        gradientPaint.setShader(gradient);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);

        // 绘制渐变背景
        canvas.drawRect(0, 0, viewWidth, viewHeight, gradientPaint);

        // 如果有选择的心情，绘制指示线和文字
        if (indicatorX >= 0) {
            // 绘制指示线
            canvas.drawLine(
                indicatorX, 
                0,  // 从色卡顶部开始
                indicatorX, 
                viewHeight + 20,  // 延伸到色卡下方
                indicatorPaint
            );

            // 绘制心情文字
            if (!currentMood.isEmpty()) {
                // 获取文字边界
                textPaint.getTextBounds(currentMood, 0, currentMood.length(), textBounds);
                
                float textX = indicatorX;
                float textY = textBounds.height() + 2; // 文字位置在色卡顶部边缘下方一点

                // 绘制文字
                canvas.drawText(currentMood, textX, textY, textPaint);
            }
        }
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
            case MotionEvent.ACTION_MOVE:
                float x = event.getX();
                updateMoodFromPosition(x);
                return true;
        }
        return super.onTouchEvent(event);
    }

    private void updateMoodFromPosition(float x) {
        // 确保x在有效范围内
        x = Math.max(0, Math.min(x, viewWidth));
        
        // 更新指示器位置
        indicatorX = x;
        
        // 计算对应的心情
        int section = (int) (x / (viewWidth / 6.0f));
        section = Math.min(section, 5);  // 确保不会超出数组范围
        
        // 更新当前心情文字
        currentMood = MOODS[section];
        
        // 通知监听器
        if (listener != null) {
            listener.onMoodSelected(currentMood);
        }
        
        invalidate();
    }

    public void setMood(String mood) {
        if (mood == null || mood.isEmpty()) {
            indicatorX = -1;
            currentMood = "";
        } else {
            for (int i = 0; i < MOODS.length; i++) {
                if (MOODS[i].equals(mood)) {
                    indicatorX = (viewWidth / 6.0f) * (i + 0.5f);
                    currentMood = mood;
                    break;
                }
            }
        }
        invalidate();
    }

    public interface OnMoodSelectedListener {
        void onMoodSelected(String mood);
    }

    public void setOnMoodSelectedListener(OnMoodSelectedListener listener) {
        this.listener = listener;
    }
} 